package Tree;

//[1,2,3,null,null,4,5]

import javax.swing.tree.TreeNode;

public class leet_offer_37 {
    public static void main(String[] args) {
        // 临时的一个空结点
        BiTreeNode root_tmp = new BiTreeNode();
        BiTreeNode root_2 = new BiTreeNode("2", root_tmp, root_tmp);
        BiTreeNode root_4 = new BiTreeNode("4", root_tmp, root_tmp);
        BiTreeNode root_5 = new BiTreeNode("5", root_tmp, root_tmp);
        BiTreeNode root_3 = new BiTreeNode("3", root_4, root_5);
        BiTreeNode root_1 = new BiTreeNode("1", root_2, root_3);

        Codec_offer_37 p = new Codec_offer_37();
        String value = p.serialize(root_1);
        System.out.println(value);

    }


}


class Codec_offer_37 {

    // Encodes a tree to a single string.
    public String serialize(BiTreeNode root) {
        // 不知道为什么好像String最后输出就是空的 但是用StringBuffer就不为空
        StringBuilder preString=new StringBuilder("");
        this.preOrder(root, preString);
        return preString.toString();
    }

    // Decodes your encoded data to tree.
    public BiTreeNode deserialize(String data) {
        return null;
    }





    /**
     * 先序遍历序列化树
     * root 树
     * preString 先序遍历的字符串
     */
    public void preOrder(BiTreeNode root, StringBuilder preString){
        if(root.data==null){
            preString.append("#!");
            return;
        }
        Object value = String.valueOf(root.data);
        preString.append(value);

        this.preOrder(root.lchild, preString);
        this.preOrder(root.rchild, preString);
    }

}
